package com.service.impl;

import com.dto.FreeRoomDTO;
import com.dto.MeetingDTO;
import com.dto.MeetingViewDTO;
import com.dto.MyMeetingDTO;
import com.mapper.AttendeeMapper;
import com.mapper.MeetingMapper;
import com.mapper.RoomMapper;
import com.pojo.Attendee;
import com.pojo.Meeting;
import com.service.MeetingService;
import com.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private MeetingMapper meetingMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private AttendeeMapper attendeeMapper;

    public boolean addMeeting(Meeting meeting) {
        int res = meetingMapper.addMeeting(meeting);
        if (res>0){
            return true;
        } else{
            return false;
        }

    }

    public List<MeetingDTO> getMeetingList(String status,int uid, int currentPageNo, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("status",status);
        map.put("uid",uid);
        map.put("currentPageNo",currentPageNo);
        map.put("pageSize",pageSize);

        return meetingMapper.getMeetingList(map);
    }

    @Override
    public List<MeetingDTO> getApprovalList(String status, int currentPageNo, int pageSize) {



        return meetingMapper.getApprovalList(status, currentPageNo, pageSize);
    }

    @Override
    public List<MyMeetingDTO> getMeetingListByUid(Integer uid, int currentPageNo, int pageSize) {
        return meetingMapper.getMeetingListByUid(uid, currentPageNo, pageSize);
    }

    public MeetingViewDTO getMeetingById(Integer id) {
        return meetingMapper.getMeetingById(id);
    }

    public int getMeetingCount(String status,Integer uid) {
        return meetingMapper.getMeetingCount(status,uid);
    }

    public boolean updateMeeting(Meeting meeting) {

        int res = meetingMapper.updateMeeting(meeting);



        if(res>0){
            return true;
        } else{
            return false;
        }

    }

    public int deleteMeetingById(Integer id) {
        attendeeMapper.delete(id);
        return meetingMapper.deleteMeetingById(id);
    }

    @Override
    public int getApprovalCount(String status) {
        return meetingMapper.getApprovalCount(status);
    }

    @Override
    public int getMeetingCountByUid(Integer uid) {
        return meetingMapper.getMeetingCountByUid(uid);
    }

    @Override
    @Transactional(readOnly = true)
    public List<FreeRoomDTO> findFreeRoom(Date useDate,Integer pageIndex) {

        Map<String, Object> map = new HashMap<>();
        map.put("useDate",useDate);
        map.put("pageIndex",(pageIndex-1)*Constants.pageSize);
        map.put("pageSize",Constants.pageSize);
        return roomMapper.getFreeRoom(map);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean applySave(Meeting meeting, List<Integer> aids) {

        int res =  meetingMapper.addMeeting(meeting);
        System.out.println(meeting);
        if (aids!=null){
            Attendee attendee = new Attendee();
            for (Integer aid : aids) {

                attendee.setMeetingid(meeting.getId());
                attendee.setUserid(aid);
                attendeeMapper.insertAttendee(attendee);
            }
        }
        return res>0?true:false;
    }

    @Override
    public int getFreeRoomCount(Date useDate) {
        return roomMapper.getFreeRoomCount(useDate);
    }
}
